package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.BannerInfoComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.BannerInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.BannerService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.BannerVo;

@Component("bannerInfoComponent")
public class BannerInfoComponentImpl implements BannerInfoComponent {
	// private static final Logger log =
	// Logger.getLogger(BannerInfoComponentImpl.class);
	// private Gson gson;
	@Resource(name = "bannerService")
	private BannerService bannerService;

	// public BannerInfoComponentImpl() {
	// gson = CommonUtil.gson();
	// }

	@Override
	public List<BannerVo> getBannerList(String body, Head head) {
		List<BannerVo> voList = new ArrayList<BannerVo>();
		List<BannerInfo> queryList = this.bannerService.query(head);
		for (BannerInfo bannerInfo : queryList) {
			BannerVo vo = new BannerVo();
			BeanUtils.copyProperties(bannerInfo, vo);
			if (StringUtils.isNotBlank(bannerInfo.getPicPath())) {
				vo.setPicPath(CommonUtil.getUrlPrefix()
						+ bannerInfo.getPicPath());
			}
			voList.add(vo);
		}
		if (voList.size() > 0) {
			return voList;
		} else {
			throw new ServerDisponseException(
					TransmissionCode.OPERATOR_EXCEPTION.getCode(),
					TransmissionCode.OPERATOR_EXCEPTION.getMessage());
		}
	}

}
